home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / prolog.arc / ANIMALS.FIL next >
Encoding:
Text File  |  1985-07-11  |  6.2 KB  |  243 lines

  1. /*=============================================================================*/
  2. inquire(has(X,Y,W,Z),T) :-
  3.     print('Does the ',X,' have ',Y,'?'),
  4.     !,nl,
  5.     get_answer(A),
  6.     respond(A,has(X,Y,W,Z),T),!.
  7. inquire(gives(X,Y,W,Z),T) :-
  8.     print('Does the ',X,' give ',Y,'?'),
  9.     !,nl,
  10.     get_answer(A),
  11.     respond(A,gives(X,Y,W,Z),T),!.
  12. inquire(eats(X,Y,W,Z),T) :-
  13.     print('Does the ',X,' eat ',Y,'?'),
  14.     !,nl,
  15.     get_answer(A),
  16.     respond(A,eats(X,Y,W,Z),T),!.
  17. inquire(flies(X,Y,W,Z),T) :-
  18.     print('Does the ',X,' fly ',Y,'?'),
  19.     !,nl,
  20.     get_answer(A),
  21.     respond(A,flies(X,Y,W,Z),T),!.
  22. inquire(swims(X,_,W,Z),T) :-
  23.     print('Does the ',X,' swim?' ),
  24.     !,nl,
  25.     get_answer(A),
  26.     respond(A,swims(X,' ',W,Z),T),!.
  27. inquire(size(X,Y,W,Z),T) :-
  28.     print('Is the ',X,' ',Y,'?'),
  29.     !,nl,
  30.     get_answer(A),
  31.     respond(A,size(X,Y,W,Z),T),!.
  32. inquire(breathing(X,Y,W,Z),T) :-
  33.     print('Does the ',X,' breath ',Y,'?'),
  34.     !,nl,
  35.     get_answer(A),
  36.     respond(A,breathing(X,Y,W,Z),T),!.
  37. inquire(vertebrate(X,_,W,Z),T) :-
  38.     print('Is the ',X,' a vertebrate?'),
  39.     !,nl,
  40.     get_answer(A),
  41.     respond(A,vertebrate(X,' ',W,Z),T),!.
  42.  
  43. respond(yes,X(A,B,[H|L],[D]),T) :-
  44.     concat(D,['     \nFACT: ',A,' ',X,' ',B]),
  45.     asserta(X(A,B,_,[D])).
  46.  
  47. respond(why,X,[]) :-
  48.     print('You should know!'),
  49.     nl,
  50.     inquire(X,[]).
  51.  
  52. respond(why,X,[H|T]) :-
  53.     print(H),
  54.     nl,!,
  55.     inquire(X,T).
  56.  
  57. has(A,B,L,M) :-
  58.     inquire(has(A,B,L,M),L),!.
  59.  
  60. gives(A,B,L,M) :-
  61.     inquire(gives(A,B,L,M),L),!.
  62. eats(A,B,L,M) :-
  63.     inquire(eats(A,B,L,M),L),!.
  64. flies(A,B,L,M) :-
  65.     inquire(flies(A,B,L,M),L),!.
  66. swims(A,_,L,M) :-
  67.     inquire(swims(A,_,L,M),L),!.
  68. size(A,B,L,M) :-
  69.     inquire(size(A,B,L,M),L),!.
  70. breathing(A,B,L,M) :-
  71.     inquire(breathing(A,B,L,M),L),!.
  72. vertebrate(A,B,L,M) :-
  73.     inquire(vertebrate(A,B,L,M),L),!.
  74. identified_as_a(A,mammal,M,L) :-
  75.     concat(Z,['\nRULE 1:  ',A,' is a mammal if it has hair.']),
  76.     append(M,[Z],X),
  77.     has(A,hair,X,W),!,
  78.     append([Z],W,L),
  79.     asserta(identified_as_a(A,mammal,M,L)).
  80. identified_as_a(A,mammal,M,L) :-
  81.     concat(Z,['\nRULE 2:  ',A,'  is a mammal if it gives milk.']),
  82.     append(M,[Z],X),
  83.     gives(A,milk,X,W),!,
  84.     append([Z],W,L),
  85.     asserta(identified_as_a(A,mammal,M,L)).
  86.  
  87. identified_as_a(A,bird,M,L) :-
  88.     concat(Z,['\nRULE 3:  ',A,' is a bird if it has feathers.']),
  89.     append(M,[Z],X),
  90.     has(A,feathers,X,W),!,
  91.     append([Z],W,L),
  92.     asserta( identified_as_a(A,bird,M,L)).
  93. identified_as_a(A,bird,M,L) :-
  94.     concat(Z,['\nRULE 4:  ',A,' is a bird if it flies.']),
  95.     append(M,[Z],X),
  96.     flies(A,well,X,W),!,
  97.     append([Z],W,L),
  98.     asserta(identified_as_a(A,bird,M,L)).
  99.  
  100. identified_as_a(A,carnivore,M,L) :-
  101.     concat(Z,['\nRULE 5:  ',A,' is a carnivore if it eats meat.']),
  102.     append(M,[Z],X),
  103.     eats(A,meat,X,W),!,
  104.     append([Z],W,L),
  105.     asserta(identified_as_a(A,carnivore,M,L)).
  106.  
  107. identified_as_a(A,reptile,M,L) :-
  108.     concat(Z,['\nRULE 6:  ',A,' is a reptile if it is an air breathing vertebrate.']),
  109.     append(M,[Z],X),
  110.     breathing(A,air,X,W),
  111.     vertebrate(A,_,X,N),
  112.     append([Z],W,R),
  113.     append(R,N,L),
  114.     asserta(identified_as_a(A,reptile,M,L)).
  115.  
  116. identified_as_a(A,tiger) :-
  117.     concat(Z,['\nRULE 7:  ',A,' is a tiger if it is a mammal,','\n',
  118.     'and a carnivore, and has black stripes']),
  119.     identified_as_a(A,mammal,[Z],M),
  120.     identified_as_a(A,carnivore,[Z],N),
  121.     has(A,'black stripes',[Z],E),
  122.     print(A,' is a tiger.'),nl,!,
  123.     append(M,N,W),
  124.     append(W,E,G),
  125.     asserta(identified_as_a(A,tiger,[Z],[Z|G])).
  126.  
  127. identified_as_a(A,ostrich) :-
  128.     concat(Z,['\nRULE 8:  ',A,' is an ostrich if it is a bird','\n',
  129.     'has a long neck and long legs.']),
  130.     identified_as_a(A,bird,[Z],M),
  131.     has(A,'a long neck',[Z],C),
  132.     has(A,'long legs',[Z],E),
  133.     print(A,' is an ostrich.'),
  134.     nl,!,
  135.     append(M,C,F),
  136.     append(F,E,G),
  137.     asserta(identified_as_a(A,ostrich,[Z],[Z|G])).
  138.  
  139. identified_as_a(A,whale) :-
  140.     concat(Z,['\nRULE 9:  ',A,' is a whale if it is a mammal','\n',
  141.     'has a gray color, swims and is huge.']),
  142.     identified_as_a(A,mammal,[Z],M),
  143.     has(A,'a gray color',[Z],N),
  144.     swims(A,_,[Z],C),
  145.     size(A,huge,[Z],D),
  146.     print(A,' is a whale.'),
  147.     nl,!,
  148.     append(M,N,W),
  149.     append(C,D,F),
  150.     append(W,F,G),
  151.     asserta(identified_as_a(A,whale,[Z],[Z|G])).
  152. identified_as_a(A,robin) :-
  153.     concat(Z,['\nRULE 10:  ',A,' is a robin if it is a bird and has a red breast.']),
  154.     identified_as_a(A,bird,[Z],M),
  155.     has(A,'a red breast',[Z],N),
  156.     print(A,' is a robin.'),
  157.     nl,!,
  158.     append(M,N,G),
  159.     asserta(identified_as_a(A,robin,[Z],[Z|G])).
  160.  
  161. identified_as_a(A,rattlesnake) :-
  162.     concat(Z,['\nRULE 11:  ',A,' is a rattlesnake if it is a reptile and has rattles.']),
  163.     identified_as_a(A,reptile,[Z],M),
  164.     has(A,'rattles',[Z],N),
  165.     print(A,' is a rattlesnake.'),
  166.     nl,!,
  167.     append(M,N,G),
  168.     asserta(identified_as_a(A,rattlesnake,[Z],[Z|G])).
  169.  
  170. how(X(A,B)) :-
  171.     X(A,B,_,[H|T]),!,
  172.     print(H),
  173.     !,
  174.     how(T),!.
  175. how([]) :-
  176.     print('\nAND THAT IS ALL THERE IS TO IT.'),
  177.     !.
  178.  
  179. how([H|T]) :-
  180.     print(H),
  181.     how(T),!.
  182.  
  183.  
  184.  
  185. help :- forgetuser,
  186.     print('Let me guess which of the following animals you have in mind:'),
  187.     nl,
  188.     print('tiger,ostrich,robin, whale, or rattlesnake.'),
  189.     nl,nl,
  190.     knowledge_domain(X),!,
  191.     deduce(X),!.
  192.  
  193. forgetuser :-
  194.     print('\nDo you want me to forget anything you have already taught me?'),
  195.     get_answer( A ),
  196.     wipemem( A ).
  197.  
  198. wipemem( A ) :- A = yes, forget( user ).
  199. wipemem( X ).
  200.  
  201. deduce([]) :-
  202.     print( 'Sorry, I don\'t know enough to guess this one.' ), !.
  203.  
  204. deduce([H|T]) :-
  205.     identified_as_a(animal,H),!,
  206.     nl,
  207.     print('Do you want to know how I concluded this? '),!,
  208.     get_answer( A ),
  209.     A = yes,
  210.     nl,
  211.     how( identified_as_a(Animal,H)),!.
  212.  
  213. deduce([H|T]) :-
  214.     !,
  215.     deduce(T).
  216.  
  217. knowledge_domain([robin,rattlesnake,ostrich,whale,tiger]).
  218.  
  219.  
  220. append([],L,L).
  221. append([Z|L1],L2,[Z|L3]) :- append( L1,L2,L3 ).
  222.  
  223.  
  224. get_answer( A ) :-
  225.     ratom( X ), name( X, String ),
  226.     valid_resp( String, A ), !.
  227.  
  228. valid_resp( [H|T], A ) :- type_ans( H, A ).
  229.  
  230. type_ans( X, A ) :- ([X] = "y"; [X] = "Y"), A = yes.
  231. type_ans( X, A ) :- ([X] = "n"; [X] = "N"), A = no.
  232. type_ans( X, A ) :- ([X] = "w"; [X] = "W"), A = why.
  233.  
  234. ?-print('\nYou can start animal by typing "help.<CR>"\n' ).
  235.  
  236. valid_resp( [], A ) :-
  237.     print('\nPlease try to give me a yes or no answer.'),
  238.     get_answer( A ), !.
  239.  
  240. valid_resp( [H|T], A ) :- valid_resp( T, A ).
  241.  
  242. /*=============================================================================*/
  243.